﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;
namespace KornyezetVedelem
{
    class Ideiglenes : DAL
    {
        private DataSet dS;
        private String strE;
        private int idK;
        public Ideiglenes(ref bool Err)
        {
            if (!base.IsConnectCreated()) //ha a kapcsolat meg nem jott letre(meg nincs letrehozva)
            {
                Err = !base.MakeConnection();
            }
        }


        //feltolti a DataReader objektumot a Nyaralok tabla NyaraloNev oszlopok mezoivel
        public System.Data.SqlClient.SqlDataReader IdeiglenesToltDR(ref string ErrM)
        {
            string sSQL = "SELECT ideigleneslerakatokid,nev,e.ellenorzokozpontokid ,e.Cim as EllenorzoKozpont FROM IdeiglenesLerakatok i, ellenorzokozpontok e where i.EllenorzoKozpontokId = e.EllenorzoKozpontokId";
            myDataReader = base.myExecuteReader(sSQL, ref ErrM);
            return myDataReader;
        }
        public DataSet IdeiglenesToltDS(ref string ErrM)
        {
            string sSQL = "SELECT ideigleneslerakatokid,nev,e.Cim as EllenorzoKozpont FROM IdeiglenesLerakatok i, ellenorzokozpontok e where i.EllenorzoKozpontokId = e.EllenorzoKozpontokId";
            dS = base.myExecuteReader_D(sSQL, ref ErrM);
            return dS;
        }
        public void Beszur(String Nev,String ellenkoz)
        {
            System.Data.SqlClient.SqlDataReader reader = IdeiglenesToltDR(ref strE);
            while (reader.Read())
            {
                if (reader["EllenorzoKozpont"].ToString() == ellenkoz)
                    Int32.TryParse(reader["EllenorzoKozpontokid"].ToString(), out idK);
            }
            reader.Close();
            CloseConnection();
            string sSQL = "SELECT MAX(IdeiglenesLerakatokID) FROM IdeiglenesLerakatok";
            int max = base.getMaximumId(sSQL) + 1;
            sSQL = "INSERT INTO IdeiglenesLerakatok values(" + max + "," + "'" + Nev + "'" + ","+idK+")";
            beszuras(sSQL);
        }
        public Exception Torol(String Id)
        {
            Exception ex1;
            String delSQL = "DELETE FROM IdeiglenesLerakatok WHERE IdeiglenesLerakatokID = " + Id;
            ex1 = Torles(delSQL);
            return ex1;

        }
        public void Modosit(String Id, String Nev,String ellenkoz)
        {
            System.Data.SqlClient.SqlDataReader reader = IdeiglenesToltDR(ref strE);
            while (reader.Read())
            {
                if (reader["ellenorzokozpont"].ToString() == ellenkoz)
                    Int32.TryParse(reader["ellenorzokozpontokid"].ToString(), out idK);
            }
            reader.Close();
            CloseConnection();

            String sSQL = "Update IdeiglenesLerakatok Set Nev = '" + Nev + "'" + ",ellenorzokozpontokid =  " + idK + "Where IdeiglenesLerakatokID = " + Id;
            modositas(sSQL);
        }
        public void feltoltCombo(ComboBox comboBox1)
        {
            MakeConnection();
            System.Data.SqlClient.SqlDataReader reader;
            reader = IdeiglenesToltDR(ref strE);

            while (reader.Read())
            {
                comboBox1.Items.Add(reader["Ellenorzokozpont"]);
            }
            reader.Close();
            CloseConnection();
        }
        public void feltoltListBox(ListBox l)
        {
            MakeConnection();
            System.Data.SqlClient.SqlDataReader reader;
            reader = IdeiglenesToltDR(ref strE);

            while (reader.Read())
            {
              
                l.Items.Add(reader["nev"]);
            }
            reader.Close();
            CloseConnection();
        }
    }
}